﻿using DDit.Component.Tools;
using System;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Security;
using System.Xml.Serialization;
using System.Configuration;
using DDit.Component.Tools.weixin;

namespace DDitApplicationFrame.Controllers
{
    
    public class ValController : ApiController
    {
        /// <summary>
        /// 接口认证
        /// </summary>
        /// <param name="echostr"></param>
        /// <param name="signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<HttpResponseMessage> wx(string echostr, string signature, string timestamp, string nonce)
        {
            string token = "mpy0";//从配置文件获取Token
            if (!WxUserManage.CheckSignature(token, signature, timestamp, nonce))
                echostr = "验证不正确";
            HttpResponseMessage responseMessage = new HttpResponseMessage { Content = new StringContent(echostr, Encoding.GetEncoding("UTF-8"), "text/plain") };

            return responseMessage;
        }
        /// <summary>
        /// 接收客服消息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<HttpResponseMessage> wx()
        {           
            var content = await Request.Content.ReadAsStringAsync();
            string strResult = WxUserManage.Rev(content); 
            return ResMsg(strResult);
        } 

        private HttpResponseMessage ResMsg(string msg)
        {
            HttpResponseMessage responseMessage = new HttpResponseMessage { Content = new StringContent(msg, Encoding.GetEncoding("UTF-8"), "text/plain") };
            return responseMessage;
        } 
        
    }
}